<template>
	<view class="address-container">
		<view class="add-address-box" v-if="JSON.stringify(address) === '{}'">
			<button type="primary" size="mini" @click="chooseAddress">请选择收货地址+</button>
		</view>
		
		<view class="change-address-box" v-else>
			<view class="row1">
				<view class="row1-left">
					<view>收货人：{{address.userName}}</view>
				</view>
				<view class="row1-right">
					<view>电话：{{address.telNumber}}</view>
					<uni-icons type="arrowright" size="16"></uni-icons>
				</view>
			</view>
			<view class="row2">
				<view>收货地址：</view>
				<view>{{addstr}}</view>
			</view>
		</view>
		
		<image src="../../static/cart_border@2x.png" class="address-border"></image>
		
	</view>
	
</template>

<script>
	
	import { mapState, mapGetters, mapMutations } from 'vuex'
	
	export default {
		name:"my-address",
		data() {
			return {
				
			};
		},

		methods: {
			...mapMutations('m_user', ['updateAddresss']),
			
			async chooseAddress() {
				// #ifdef MP-WEIXIN
				const [err, res] = await uni.chooseAddress().catch(err => err)

				//点击确认后
				if (err === null && res.errMsg === 'chooseAddress:ok') {
					this.updateAddresss(res)
				}
				
				//chooseAddress:fail auth deny 安卓
				//chooseAddress:fail authorize no response 苹果
				if (err && (res.errMsg == 'chooseAddress:fail auth deny' || err.errMsg === 'chooseAddress:fail authorize no response')) {
					this.reAuth()
				}
				// #endif
			},
			
			// 让用户重新授权
			reAuth() {
				uni.showModal({
				    title: '提示',
				    content: '检测到您没打开地址权限，是否去设置打开？',
				    success: function (res) {
				        if (res.confirm) {
				            console.log('用户点击确定');
				        } else if (res.cancel) {
				            console.log('用户点击取消');
				        }
				    }
				});
			}

		},
		
		computed: {
			...mapState('m_user', ['address']),
			...mapGetters('m_user', ['addstr'])
		}
	}
</script>

<style lang="scss">
.address-container {
	padding: 0px 5px;
}
.add-address-box {
	height: 90px;
	width: 100%;
	display: flex;
	flex-direction: column;
	justify-content: center;
	align-items: center;

	button {
		color: #ffffff;
		font-size: 13px;
		background-color: #4CD964;
		text-align: center;
		height: 30px;
		width: 150px;
	}
}

.change-address-box {
	padding: 5px 0;
	font-size: 12px;
	height: 90px;
	width: 100%;
	display: flex;
	flex-direction: column;
	justify-content: center;
	padding: 0 5px;
	
	.row1 {
		display: flex;
		justify-content: space-between;
		.row1-right {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
	
	.row2 {
		margin-top: 10px;
		display: flex;
		align-items: center;
		.row2-left {
			white-space: nowrap;
		}

	}
}

.address-border {
	width: 100%;
	height: 5px;
	display: block;
}

</style>
